﻿
Partial Public Class Truck_Licence_Master
    Inherits DB_Class

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "TRUCK LICENCE MASTER"
            bindData()
            ViewState("editID") = ""
        Else
            ViewState("Licence") = Nothing

        End If

    End Sub
    Protected Sub bindData(Optional ByVal searchText As String = "")
        queryString = "select * from Truck_Licence_Master tl left join Truck_Type_Master tt on tl.TruckTypeID=tt.TruckTypeID where tl.isDel<>'YES'"
        If searchText <> "" Then
            queryString &= " and TruckLicence like '%" & searchText & "%'"
        End If
        Dim dt As DataTable = getTable("licenceMaster")
        ViewState("Licence") = dt
        licenceGrid.DataSource = dt
        licenceGrid.DataBind()
    End Sub
    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = licenceGrid.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                licenceGrid.PageIndex = 0
            Case "prev"
                If (licenceGrid.PageIndex <> 0) Then
                    licenceGrid.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (licenceGrid.PageIndex <> licenceGrid.PageCount) Then
                    licenceGrid.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                licenceGrid.PageIndex = licenceGrid.PageCount
        End Select
        licenceGrid.DataSource = ViewState("Licence")
        licenceGrid.DataBind()
    End Sub
    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = licenceGrid.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        licenceGrid.PageIndex = ddlPage.SelectedIndex
        licenceGrid.DataSource = ViewState("Licence")
        licenceGrid.DataBind()
    End Sub
    Protected Sub BindTruckType()
        Dim db As New DB_Class()
        db.queryString = "select * from Truck_Type_Master where isDel <> 'YES'"
        Dim dt As DataTable = db.getTable("TruckType")
        ddlTruckType.DataSource = dt
        ddlTruckType.DataTextField = "TruckType"
        ddlTruckType.DataValueField = "TruckTypeID"
        ddlTruckType.DataBind()
        ddlTruckType.Items.Insert(0, New ListItem("-= เลือกประเภทรถบรรทุก =-", ""))
    End Sub

    Private Sub btnAddLicence_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddLicence.Click
        tableDataPanel.Visible = False
        formPanel.Visible = True
        BindTruckType()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim qStr As String = Request.Url.Query
        Response.Redirect("~/Master_Form/Truck_Licence_Master.aspx" + qStr)
    End Sub

    Private Sub licenceGrid_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles licenceGrid.RowDeleting
        Dim truckLicenceID As String = licenceGrid.DataKeys(e.RowIndex)("TruckLicenceID").ToString()
        queryString = "update Truck_Licence_Master set isDel='YES' where TruckLicenceID='" + truckLicenceID + "'"
        insertUpdate()
        Dim qStr As String = Request.Url.Query
        Response.Redirect("~/Master_Form/Truck_Licence_Master.aspx" + qStr)
    End Sub

    Private Sub licenceGrid_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles licenceGrid.RowEditing
        tableDataPanel.Visible = False
        formPanel.Visible = True
        Dim truckLicenceID As String = licenceGrid.DataKeys(e.NewEditIndex)("TruckLicenceID").ToString()
        ViewState("editID") = truckLicenceID
        Dim licence As String = licenceGrid.DataKeys(e.NewEditIndex)("TruckLicence").ToString()
        Dim tailerLicence As String = licenceGrid.DataKeys(e.NewEditIndex)("TailerLicence").ToString()
        Dim weight As String = licenceGrid.DataKeys(e.NewEditIndex)("Weight").ToString()
        Dim pieces As String = licenceGrid.DataKeys(e.NewEditIndex)("Pieces").ToString()
        Dim truckType As String = licenceGrid.DataKeys(e.NewEditIndex)("TruckTypeID").ToString()
        txtLicence.Text = licence
        txtTailerLicence.Text = tailerLicence
        txtWeight.Text = weight
        txtPieces.Text = pieces
        BindTruckType()
        ddlTruckType.ClearSelection()
        ddlTruckType.Items.FindByValue(truckType).Selected = True
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim qStr As String = Request.Url.Query
        If CType(ViewState("editID"), String) = "" Then
            Dim truckLicenceId As String = GeneratID("Truck_Licence_Master", "TruckLicenceID")
            queryString = String.Format("insert into Truck_Licence_Master (TruckLicenceID,TruckLicence,Weight,Pieces,TruckTypeID,TailerLicence) values('{0}','{1}',{2},'{3}','{4}','{5}')" _
                                        , truckLicenceId, txtLicence.Text.Trim(), txtWeight.Text, txtPieces.Text, ddlTruckType.SelectedValue, txtTailerLicence.Text.Trim())
            If insertUpdate() <> "" Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาระบุข้อมูลให้ถูกต้อง !!');", True)
                Exit Sub
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลสำเร้จ');" & "window.location='Truck_Licence_Master.aspx" & qStr & "'", True)
                Response.Redirect("~/Master_Form/Truck_Licence_Master.aspx" + qStr)
            End If
        Else
            Dim truckLicenceId As String = ViewState("editID").ToString()
            queryString = String.Format("update Truck_Licence_Master set TruckLicence='{0}',Weight={1},Pieces='{2}',TruckTypeID='{3}',TailerLicence='{4}'", txtLicence.Text.Trim(), txtWeight.Text, txtPieces.Text, ddlTruckType.SelectedValue, txtTailerLicence.Text.Trim()) + " where TruckLicenceID='" + truckLicenceId + "'"
            If insertUpdate() <> "" Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาระบุข้อมูลให้ถูกต้อง !!');", True)
                Exit Sub
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลสำเร้จ');" & "window.location='Truck_Licence_Master.aspx" & qStr & "'", True)
                ' Response.Redirect("~/Master_Form/Truck_Licence_Master.aspx" + qStr)
            End If
        End If


    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        bindData(txtLicenceSearch.Text)
    End Sub
End Class